replace estimate_extraction.R with faster version#8
Open
jimstigler wants to merge 1 commit intomainfrom
Open
Conversation
I am trying to speed up the f and pre functions. Claude helped me speed it up by 5x. I'll make a pull request and include a fuller writeup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Performance: Add fast paths for f(), pre(), and p() extraction functions
Problem
The
f(),pre(), andp()functions are extremely slow when used in bootstrapping loops because they always build a fullsupernova()ANOVA table, even when the user only needs the overall model statistic.Benchmark
This represents a ~5x slowdown that significantly impacts the classroom experience when students are running bootstrap simulations.
Root Cause
The extraction functions always call
extract_stat(), which internally builds a completesupernova()table:Building the supernova table involves:
drop1()This is massive overkill when the user only needs the overall model F statistic.
Solution
Add fast paths that bypass
supernova()for the most common use case (extracting overall model statistics with default arguments):Similar optimizations for:
pre()→ usessummary(fit)$r.squaredp()→ computes directly viapf(F, df1, df2, lower.tail = FALSE)Changes
Modified:
R/estimate_extraction.Rpfto@importFrom statsf(): Fast path usingsummary(fit)$fstatistic[1]pre(): Fast path usingsummary(fit)$r.squaredp(): Fast path computing p-value directly from F statisticBackward Compatibility
✅ Fully backward compatible. The fast path only activates when:
all = FALSE(the default)predictoris not specified (the default)All other parameter combinations continue to use the full
supernova()path.Tests
Impact
do(1000) * f(...)practical for classroom use